package org.acerge.pieces;

import java.awt.Image;
import java.awt.Toolkit;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.Serializable;

public class PiecesObject implements Serializable{
	private static final long serialVersionUID = -8909096566771666564L;
	//pieceName:[a,A=advisor],[b,B=bishop],[k,K=king],[p,P=pawn]
	//[r,R=rook],[c,C=cannon],[n,N=knight],Capital=White
	private int[] piecesType=null;
	private byte[][] pictureData=null;
	private byte[] header=null;
	private boolean decoded=false;
	private PiecesObject instance;
	public PiecesObject(){}
	public PiecesObject(byte[] h,int[] piecesType,byte[][] pictureData){
		this.header=h;
		this.piecesType=piecesType;
		this.pictureData=pictureData;
	}
	public int[] getPiecesType(){
		return piecesType;
	}
	public Image getImage(int pieceType){
		return Toolkit.getDefaultToolkit().createImage(pictureData[pieceType]);
	}
	public byte[] getImageContent(int pieceType) {
		return pictureData[pieceType];
	}
	public PiecesObject getInstance(){
		if(instance==null) {
			try (InputStream stream = PiecesObject.class.getClassLoader().getResourceAsStream("data/pieceObj");
					ObjectInputStream ois = new ObjectInputStream(stream);){
				instance=(PiecesObject)ois.readObject();
				return instance;
			} catch (IOException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		}
		return instance;
	}
	public byte[] getHeader() {
		return header;
	}
	public boolean isDecoded() {
		return decoded;
	}
}
